using newtelligence.DasBlog.Web.Services;

namespace newtelligence.DasBlog.Web
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;
    using newtelligence.DasBlog.Runtime;
    using newtelligence.DasBlog.Web.Core;

	/// <summary>
	///		Summary description for DeleteEntryBox.
	/// </summary>
	public class DeleteEntryBox : System.Web.UI.UserControl
	{
        protected System.Web.UI.WebControls.Button yesButton;
        protected System.Web.UI.WebControls.Button noButton;
        protected System.Web.UI.WebControls.Label Label1;
        protected System.Web.UI.HtmlControls.HtmlGenericControl deleteitem;
        protected System.Resources.ResourceManager resmgr;


		private void Page_Load(object sender, System.EventArgs e)
		{
            if (SiteSecurity.IsInRole("admin") == false) 
            {
                Response.Redirect("~/FormatPage.aspx?path=SiteConfig/accessdenied.format.html");
            }

            resmgr = ((System.Resources.ResourceManager)ApplicationResourceTable.Get());

            if ( !IsPostBack )
            {
                Session["newtelligence.DasBlog.Web.DeleteEntryBox.OriginalReferrer"] = Request.UrlReferrer;
            }

            DataBind();
		}



		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.yesButton.Click += new System.EventHandler(this.yesButton_Click);
            this.noButton.Click += new System.EventHandler(this.noButton_Click);
            this.Load += new System.EventHandler(this.Page_Load);
            this.PreRender += new System.EventHandler(this.DeleteEntryBox_PreRender);

        }
		#endregion

        private void yesButton_Click(object sender, System.EventArgs e)
        {
            SharedBasePage requestPage = Page as SharedBasePage;
            string entryId = requestPage.WeblogEntryId;
            
            if (SiteSecurity.IsInRole("admin") == false) 
            {
                Response.Redirect("~/FormatPage.aspx?path=SiteConfig/accessdenied.format.html");
            }
            else
            {
				//requestPage.DataService.DeleteEntry( entryId, requestPage.SiteConfig.CrosspostSites );
            	EditServiceImplementation.DeleteEntry(entryId, requestPage.SiteConfig, requestPage.LoggingService, requestPage.DataService);
            }
            
            if ( Session["newtelligence.DasBlog.Web.DeleteEntryBox.OriginalReferrer"] != null )
            {
                Uri originalReferrer = Session["newtelligence.DasBlog.Web.DeleteEntryBox.OriginalReferrer"] as Uri;
                Session.Remove("newtelligence.DasBlog.Web.DeleteEntryBox.OriginalReferrer");                   
				Response.Redirect(originalReferrer.AbsoluteUri, true);
            }
            else
            {
				 Response.Redirect(Utils.GetAdminPageUrl(), true);
            }
        }

        private void noButton_Click(object sender, System.EventArgs e)
        {
            SharedBasePage requestPage = Page as SharedBasePage;
            if ( Session["newtelligence.DasBlog.Web.DeleteEntryBox.OriginalReferrer"] != null )
            {
                Uri originalReferrer = Session["newtelligence.DasBlog.Web.DeleteEntryBox.OriginalReferrer"] as Uri;
                Session.Remove("newtelligence.DasBlog.Web.DeleteEntryBox.OriginalReferrer");                   
                requestPage.Redirect( originalReferrer.AbsoluteUri );
            }
            else
            {
                requestPage.Redirect(Utils.GetStartPageUrl(requestPage.SiteConfig));
            }
        }

        private void DeleteEntryBox_PreRender(object sender, System.EventArgs e)
        {
            SharedBasePage requestPage = Page as SharedBasePage;
            string entryId = requestPage.WeblogEntryId;
        
            Entry entry = requestPage.DataService.GetEntry(entryId);
            if ( entry != null )
            {
                requestPage.ProcessItemTemplate(entry,deleteitem);
            }
         
        }
	}
}
